package cn.daniellee.plugin.pm;

import cn.daniellee.plugin.pm.command.MissionCommand;
import cn.daniellee.plugin.pm.command.MissionCompleter;
import cn.daniellee.plugin.pm.component.ItemGenerator;
import cn.daniellee.plugin.pm.component.PlaceholderHook;
import cn.daniellee.plugin.pm.core.MissionCore;
import cn.daniellee.plugin.pm.listener.MenuListener;
import cn.daniellee.plugin.pm.listener.PlayerListener;
import cn.daniellee.plugin.pm.runnable.MissionRunnable;
import cn.daniellee.plugin.pm.storage.MysqlStorage;
import cn.daniellee.plugin.pm.storage.Storage;
import cn.daniellee.plugin.pm.storage.SqliteStorage;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

public class PartnerMission extends JavaPlugin {

    private static PartnerMission instance;

    private String prefix;

    private Storage storage;

    private BukkitTask missionTask;

    private boolean completeBoardCast;

    private boolean hasCMI;

    private boolean hasResidence;

    private boolean hasSkinsRestorer;

    @Override
    public void onEnable() {
        instance = this;

        if (loadConfig()) {
            getLogger().info(" ");
            getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> PartnerMission Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
            getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
            getLogger().info(" ");

            Bukkit.getPluginManager().registerEvents(new MenuListener(), this);
            Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);

            Bukkit.getPluginCommand("partnermission").setExecutor(new MissionCommand());
            Bukkit.getPluginCommand("partnermission").setTabCompleter(new MissionCompleter());

            missionTask = new MissionRunnable().runTaskTimerAsynchronously(this, 0, 20); // 1秒执行一次

            hasCMI = Bukkit.getPluginManager().getPlugin("CMI") != null;
            hasResidence = Bukkit.getPluginManager().getPlugin("Residence") != null;
            hasSkinsRestorer = Bukkit.getPluginManager().getPlugin("SkinsRestorer") != null;

            if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
                new PlaceholderHook(this).register();
                getLogger().info(" ");
                getLogger().info(">>>>>>>>>>>>>>>>>>>> Successfully hook to PlaceholderAPI <<<<<<<<<<<<<<<<<<<".replace("&", "§"));
                getLogger().info(" ");
            }
        }
    }

    public boolean loadConfig() {
        getLogger().info("[PartnerMission] Loading config...");
        if(!getDataFolder().exists()) getDataFolder().mkdirs();
        if (storage != null) storage.close();
        storage = getConfig().getBoolean("storage.mysql.use", false) ? new MysqlStorage() : new SqliteStorage();
        if (storage.initialize()) {
            getLogger().info("[PartnerMission]Storage initialized.");
        } else {
            getLogger().info(" ");
            getLogger().info("[PartnerMission]Initializing data store failed, please edit the config and reload the plugin.".replace("&", "§"));
            getLogger().info(" ");
            Bukkit.getPluginManager().disablePlugin(this);
            return false;
        }
        prefix = "&7[&6" + getConfig().getString("prompt-prefix", "PartnerMission") + "&7] &3: &r";
        ItemGenerator.disableSkullLoad = getConfig().getBoolean("setting.disable-skull-load", false);
        completeBoardCast = getConfig().getBoolean("setting.complete-boardcast", true);
        saveDefaultConfig();
        MissionCore.loadMissionAndReward();
        return true;
    }

    @Override
    public void onDisable() {
        if (missionTask != null && !missionTask.isCancelled()) missionTask.cancel();
        storage.close();
        getLogger().info(" ");
        getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> PartnerMission Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
        getLogger().info(" ");
    }

    public static PartnerMission getInstance() {
        return instance;
    }

    public String getPrefix() {
        return prefix;
    }

	public Storage getStorage() {
		return storage;
	}

    public boolean isCompleteBoardCast() {
        return completeBoardCast;
    }

    public boolean hasCMI() {
        return hasCMI;
    }

    public boolean hasResidence() {
        return hasResidence;
    }

    public boolean hasSkinsRestorer() {
        return hasSkinsRestorer;
    }
}
